package com.ruoyi.system.quartz;


import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.system.domain.SysMessage;
import com.ruoyi.system.mapper.SysMessageMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component("MessageTask")
@EnableScheduling
public class MessageTask {

    private static final Logger log = LoggerFactory.getLogger(MessageTask.class);

    @Autowired
    private SysMessageMapper sysMessageMapper;

    //留言自动关闭（用户超过2天没有新回复，则留言自动关闭）
//    @Scheduled(cron = "0 0/1 * * * ? ") //每分钟执行一次
        @Scheduled(cron = "0 0 0 1/2 * ? ") //每两天执行一次
    private void messageAutoEndTask(){
        try {
            //获取超过2天没有新回复的留言的id
            List<Long> list = sysMessageMapper.selectExceedTowDayMessage();
            if(CollectionUtil.isNotEmpty(list)){
                //批量修改状态
                sysMessageMapper.batchUpdateMessageStatus(list);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("自动关闭留言失败："+ e.getMessage());
        }
    }
}
